Get the newest 5 articles out of hundreds -- a super efficient sort in XSLT

By  Dimitre Novatchev
 
Language  XSLT
Category designpatterns, xml, msxml
Posted 26  Feb  2001
Updated 05  Mar  2001
 
Summary
This snippet shows how given a set of name/value pairs for a record from a set of records, to "reconstitute" the whole records.
 
This problem was first formulated by my friend Corey Haines. Given a set of name/value pairs for a record from a set of records, we must "reconstitute" the whole records.
 
For example, given the xml document specified in this snippet:
 
<root>
  <data>
    <item id='1' name='age' value='25' />
    <item id='2' name='age' value='27' />
    <item id='2' name='cool' value='no' />
    <item id='1' name='cool' value='yes' />
  </data>
</root>
the result will be:
id age cool
1 25 yes
2 27 no
This snippet illustrates selecting distinct values by sorting, and also how to generate "plain text".
 
It also demonstrates how to produce a comma-separated list of items (every row).
 
Note how two xsl:variables are defined and used in two nested xsl:for-each loops in order to produce the two-dimensional table.
 
XML Source
 
 
XSL Stylesheet
 
 
XML/HTML Result:
 
 
Text Result: